package mysql

import (
	"doctor-medicine-system/doctorrpc/doctorrpc"
	"gorm.io/gorm"
)

type Medicine struct {
	gorm.Model
	MedicineName string `gorm:"type:varchar(50);not null;comment:'药品名称'"`
	Size         string `gorm:"type:varchar(50);not null;comment:'规格'"`
	Where        string `gorm:"type:varchar(100);not null;comment:'生产厂商'"`
	Methods      string `gorm:"type:varchar(100);not null;comment:'用法用量'"`
	BadMethods   string `gorm:"type:varchar(100);not null;comment:'主演事项'"`
}

func (m *Medicine) MedicineAdd(db *gorm.DB) error {
	return db.Debug().Create(m).Error
}

func (m *Medicine) MedicineFind(db *gorm.DB, id int64) error {
	return db.Debug().Where("id=?", id).First(m).Error
}

func (m *Medicine) MedicineUpdate(db *gorm.DB) error {
	return db.Debug().Updates(m).Error
}

func MedicineFindByNamne(db *gorm.DB, name string) ([]*doctorrpc.MedicineList, error) {
	var list []*doctorrpc.MedicineList
	err := db.Debug().Model(&Medicine{}).Select("id as medicine_id", "medicine_name", "size",
		"where", "methods", "bad_methods").
		Where("medicine_name like ?", "%"+name+"%").Find(&list).Error
	return list, err
}
